package com.imut.hcdp.job;

import com.imut.hcdp.domain.HcdpOrder;
import com.imut.hcdp.domain.HcdpTrainRoute;
import com.imut.hcdp.domain.HcdpTrainRouteDetail;
import com.imut.hcdp.mapper.HcdpOrderMapper;
import com.imut.hcdp.mapper.HcdpTrainRouteDetailMapper;
import com.imut.hcdp.mapper.HcdpTrainRouteMapper;
import com.imut.hcdp.service.IHcdpPassengerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;

/**
 * 订单任务
 */
@Component
@Slf4j
public class OrderJob {

    @Autowired
    private HcdpOrderMapper hcdpOrderMapper;

    @Autowired
    private HcdpTrainRouteDetailMapper hcdpTrainRouteDetailMapper;

    @Autowired
    private HcdpTrainRouteMapper hcdpTrainRouteMapper;

    @Autowired
    private IHcdpPassengerService iHcdpPassengerService;

    /**
     * 轮询未支付订单,修改为取消,每两分钟执行一次
     */
    @Scheduled(cron = "0 0/2 * * * ?")
    public void notPayOrder(){
        log.info("notPayOrder 任务轮询开始");
        int count = 0;
        //获取所有未支付订单
        List<HcdpOrder> hcdpOrders = hcdpOrderMapper.selectHcdpOrderList(new HcdpOrder().setStatus(1L));
        if(CollectionUtils.isEmpty(hcdpOrders)){
            return;
        }
        for (int i = 0; i < hcdpOrders.size(); i++) {
            HcdpOrder hcdpOrder = hcdpOrders.get(i);
            Long crttime = hcdpOrder.getCrttime().getTime();
            Long now = System.currentTimeMillis();
            //订单超过五分钟则取消
            if(now - crttime > 1000 * 60 * 5){
                hcdpOrderMapper.updateHcdpOrder(new HcdpOrder().setId(hcdpOrder.getId()).setStatus(4L));
                /**
                 * 未支付的订单直接删除乘车人信息
                 */
                //iHcdpPassengerService.deleteHcdpPassengerByOrderId(hcdpOrder.getId());
                count++;
            }
        }
        log.info("=============本轮【未支付订单】取消个数：{}==============",count);

    }

    /**
     * 支付成功的，如果行程已完成状态改变
     */
    @Scheduled(cron = "0 0/5 * * * ?")
    public void finalPayOrder(){
        log.info("finalPayOrder 任务轮询开始");
        int count = 0;
        //获取所有已支付订单
        List<HcdpOrder> hcdpOrders = hcdpOrderMapper.selectHcdpOrderList(new HcdpOrder().setStatus(2L));
        if(CollectionUtils.isEmpty(hcdpOrders)){
            return;
        }
        for (int i = 0; i < hcdpOrders.size(); i++) {
            HcdpOrder hcdpOrder = hcdpOrders.get(i);
            Long endTime = 0L;
            if(hcdpOrder.getRouteDetailId() != null){
                HcdpTrainRouteDetail hcdpTrainRouteDetail = hcdpTrainRouteDetailMapper.selectHcdpTrainRouteDetailById(1L);
                endTime = hcdpTrainRouteDetail.getETime().getTime();
            }else{
                HcdpTrainRoute hcdpTrainRoute = hcdpTrainRouteMapper.selectHcdpTrainRouteById(hcdpOrder.getRouteId());
                endTime = hcdpTrainRoute.getETime().getTime();
            }
            Long now = System.currentTimeMillis();
            //当前时间大于行程结束时间，订单变为已完成
            if(now >= endTime){
                hcdpOrderMapper.updateHcdpOrder(new HcdpOrder().setId(hcdpOrder.getId()).setStatus(3L));
                count++;
            }
        }
        log.info("==========本轮【行程完成订单】个数：{}==========",count);
    }



}
